package com.df.excel_export.validator.vadator_impl.type_long;

import com.df.excel_export.common.ExcelTable;
import com.df.excel_export.validator.AbstractIFieldValidator;
import com.df.excel_export.validator.IFieldValidator;
import com.df.excel_export.validator.IValidatorFactory;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class LongInValidator extends AbstractIFieldValidator implements IValidatorFactory {
    public Set<Integer> valueSet = new HashSet<>();
    @Override
    public boolean support(String filedType, String ruleStr) {
        if(!filedType.trim().equals("long")){
            return false;
        }
        String trim = ruleStr.trim();
        if(trim.startsWith("in(") && trim.endsWith(")")){
            return true;
        }
        return false;
    }

    @Override
    public IFieldValidator create(String filedType, String ruleStr) {
        LongInValidator validator  = new LongInValidator();
        String trim = ruleStr.trim();
        String newTrim = trim.substring(3, ruleStr.length() - 1);
        String[] split = newTrim.split(",");
        for(String s : split){
            validator.valueSet.add(Integer.parseInt(s));
        }
        return validator;
    }

    @Override
    public Integer getPriority() {
        return 0;
    }

    @Override
    public boolean validate(ExcelTable checkTable, List<Object> rowData, Integer dataDowIndex, Integer fieldIndex, String fieldName, String fieldTypeStr, Object filedValue) {
        Integer value = (Integer) filedValue;
        if(!valueSet.contains(value)){
            return false;
        }
        return true;
    }
}
